//
//  BCSwitchAccountController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/25.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCSwitchAccountController.h"

#import "BCSwitchAccountCell.h"

@interface BCSwitchAccountController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;

/**
 *contentHeight
 */
@property (nonatomic, assign) CGFloat contentHeight;

/**
 *userInfo
 */
@property (nonatomic, strong) UserInfoModel *userInfo;

/**
 *infoTable
 */
@property (nonatomic, strong) UITableView *infoTable;

/**
 *cancelButton
 */
@property (nonatomic, strong) QMUIButton *cancelButton;

/**
 *BCSwitchAccountComplte
 */
@property (nonatomic, copy) BCSwitchAccountComplte completeBlock;

@end

@implementation BCSwitchAccountController


+ (void)showSwitchAccountFromController:(UIViewController *)fromController userInfo:(UserInfoModel *)userInfo complete:(BCSwitchAccountComplte)complete {
    BControllerWindow *window = [[BControllerWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //
    BCSwitchAccountController *vc = [BCSwitchAccountController new];
    vc.showWindow = window;
    vc.fromController = fromController;
    vc.userInfo = userInfo;
    vc.completeBlock = complete;
    //
    window.rootViewController = vc;
    [window makeKeyAndVisible];
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self handleDataSource];
    //
    [self setupSubviews];
    //
    [self showContentView];
}
- (void)handleDataSource {
    [self.dataSource removeAllObjects];
    NSArray *infos = [BCUserLoginManager sharedLogin].loginedUserInfos;
    if (self.userInfo) {
        for (int i=0; i<infos.count; i++) {
            UserInfoModel *model = infos[i];
            if (![model.sub isEqualToString:self.userInfo.sub]) {
                [self.dataSource addObject:model];
            }
        }
    }
    //暂时展示5个cell 高度
    CGFloat singleH = [BCSwitchAccountCell defaultHeight];
    self.contentHeight = 0;
    self.contentHeight += singleH*(self.dataSource.count<=5?self.dataSource.count:5);
    //
    self.contentHeight += singleH;
    //
    self.contentHeight += Height_Bottom_SafeArea;
}


- (void)setupSubviews {
    [self.view addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    //
    CGFloat contentHeight = self.contentHeight;
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(contentHeight);
        make.bottom.mas_equalTo(self.view).offset(contentHeight);
    }];
    [self setContents];
    //
    [self.view layoutIfNeeded];
}
- (void)setContents {
    self.cancelButton = [QMUIButton new];
    [self.cancelButton setTitle:BCLocalizedKey(@"Cancel") forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.contentView).offset(-Height_Bottom_SafeArea);
        make.left.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo([BCSwitchAccountCell defaultHeight]);
    }];
    [self.cancelButton addTarget:self action:@selector(dismissContentView) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.infoTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.infoTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.infoTable.dataSource = self;
    self.infoTable.delegate = self;
    [self.contentView addSubview:self.infoTable];
    //
    [self.infoTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.cancelButton.mas_top);
    }];
    
    [self.infoTable registerClass:BCSwitchAccountCell.class forCellReuseIdentifier:NSStringFromClass(BCSwitchAccountCell.class)];
}

- (void)showContentView {
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset(0);
    }];
    [UIView animateWithDuration:.2 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}
- (void)dismissContentView {
    CGFloat contentHeight = self.contentHeight;
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset(contentHeight);
    }];
    [UIView animateWithDuration:.2 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self dismissWindow];
    }];
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCSwitchAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BCSwitchAccountCell.class)];
    cell.userInfo = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BCSwitchAccountCell defaultHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UserInfoModel *userInfo = self.dataSource[indexPath.row];
    if (self.completeBlock) {
        self.completeBlock(userInfo);
    }
    [self dismissContentView];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    //
    [self dismissContentView];
}

@end
